package cn.pegasus.teamwork.appinterface.controller.washer;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.iyizhan.teamwork.base.util.ObjectUtil;
import cn.pegasus.teamwork.appinterface.base.BaseController;
import cn.pegasus.teamwork.appinterface.util.ParameterEunm;
import cn.pegasus.teamwork.appinterface.util.ReturnRsUtils;
import cn.pegasus.teamwork.pegasus.entity.BussWasherFunddetails;
import cn.pegasus.teamwork.pegasus.entity.BussWasherFundstatistics;
import cn.pegasus.teamwork.pegasus.service.BussWasherFunddetailsService;
import cn.pegasus.teamwork.pegasus.service.BussWasherFundstatisticsService;
import cn.pegasus.teamwork.pegasus.service.BussWasherSginService;
import cn.pegasus.teamwork.util.DateCompareUtil;

/**
 * @ClassName: SysUcenerUserController
 * @Description: 用户控制中心
 * @author ryan.bian
 * @date 2017年11月12日 上午10:10:32
 * 
 *       包括以下主要几个接口：
 * 
 *       =========================== 1.我的消息 2.我的账户 3.我的资金明细 4.我的优惠券 5.优惠券详情
 *       6.意见反馈 7.我的积分
 * 
 *       =====================================
 * 
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@Controller
@RequestMapping("/wct/*")
public class WasherCetInfoController extends BaseController {

	private static final Logger LOGGER = LoggerFactory.getLogger(WasherCetInfoController.class);

	/**
	 * 账户资金总表
	 */
	@Autowired
	private BussWasherFundstatisticsService fundstatisticsService;

	/**
	 * 账户明细列表
	 */
	@Autowired
	private BussWasherFunddetailsService funddetailsService;
	
	@Autowired
	private BussWasherSginService washerSginService;

	/**
	 * 我的账户
	 * 
	 * @param userName
	 * @param password
	 * @param app_code
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/awd", method = RequestMethod.POST)
	public Map<String, Object> accountWithdrawals(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		map.put("washerId", getSessionWasherId());
		try {
			BussWasherFundstatistics bus = fundstatisticsService.detail(map);
			Map<String, Object> tMap = new HashMap<String, Object>();
			if (bus != null) {
				tMap.put("balance", bus.getBalance());
				tMap.put("washerBalance", bus.getWasherBalance());
				tMap.put("rewardBalance", bus.getRewardBalance());
				Map<String, Object> rsMap = new HashMap<String, Object>();
				rsMap.put("rs", tMap);
				resultMap =  ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE,rsMap);
			} else {
				resultMap =  ReturnRsUtils.resultBackString(ParameterEunm.ERROR_NULL_RESULT_CODE,null);
			}
		} catch (Exception e) {
			LOGGER.error("服务器异常!", e);
			resultMap =  ReturnRsUtils.resultBackString(ParameterEunm.FAILED_CODE,null);
		}
		return resultMap;
	}
	
	/**
	 * 我的账户提现申请
	 * 
	 * @param userName
	 * @param password
	 * @param app_code
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/wwd", method = RequestMethod.POST)
	public Map<String, Object> washerWithdrawals(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		map.put("washerId", getSessionWasherId());
		try {			
			BussWasherFundstatistics bus = fundstatisticsService.detail(map);
			if (bus != null) {
				bus.setExpenditureBalance(bus.getBalance());
				fundstatisticsService.update(bus);
				
				Map<String, Object> tMap = new HashMap<String, Object>();
				tMap.put("washerId", getSessionWasherId());
				tMap.put("capitalType", 10);
				tMap.put("transactionTime", new Date());
				tMap.put("payBalance", bus.getBalance());
				tMap.put("ispaymoney", 1);
				tMap.put("examineStatus", 1);
				tMap.put("status", 1);
				funddetailsService.add(tMap);
				
				resultMap =  ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE,true);
			} else {
				resultMap =  ReturnRsUtils.resultBackString(ParameterEunm.ERROR_NULL_RESULT_CODE,null);
			}
		} catch (Exception e) {
			LOGGER.error("服务器异常!", e);
			resultMap =  ReturnRsUtils.resultBackString(ParameterEunm.FAILED_CODE,null);
		}
		return resultMap;
	}

	/**
	 * 洗车工签到
	 * 
	 * @param phone_number
	 * @param app_code
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/sg", method = RequestMethod.POST)
	public Map<String, Object> sign(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			boolean isNotNull = ObjectUtil.checkObjectFile(new String[] { "param" }, map);
			if (isNotNull) {
				// 参数解析
				String  param = map.get("param").toString();
				String[] params = param.split(";");
				if(params.length > 0){
					for (int i = 0; i < params.length; i++) {
						String par = params[i];
						String[] pars = par.split(",");
						String date = pars[0];
						String morning = pars[1];
						String afternoon = pars[2];
						Map<String, Object> tMap = new HashMap<String, Object>();
						tMap.put("washerId", getSessionWasherId());
						tMap.put("sginDate", DateCompareUtil.getDateToString(date));
						tMap.put("sginMorning", morning);
						tMap.put("sginAfternoon", afternoon);
						tMap.put("washerStatus", 2);
						washerSginService.detail(tMap);
					}
					resultMap =  ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE,true);					
				} else {
					resultMap =  ReturnRsUtils.resultBackString(ParameterEunm.ERROR_NULL_RESULT_CODE,null);
				}
			} else {
				resultMap =  ReturnRsUtils.resultBackString(ParameterEunm.ERROR_PARAMS_CODE,null);
			}
		} catch (Exception e) {
			resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_500_CODE, null);
		}

		return resultMap;
	}
	
	/**
	 * 洗车工提现明细
	 * 
	 * @param id
	 * @param map
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/wwl", method = RequestMethod.POST)
	public Map<String, Object> withdrawalsList(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		map.put("washerId", getSessionWasherId());
		map.put("capitalType",10);
		try {
			List<BussWasherFunddetails> bus = funddetailsService.query(map);
			if (bus != null && bus.size() > 0) {
				Map<String, Object> mape = new HashMap<String, Object>();
            	mape.put("lis", bus);
				resultMap =  ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE,mape);
			} else {
				resultMap =  ReturnRsUtils.resultBackString(ParameterEunm.ERROR_NULL_RESULT_CODE,null);
			}
		} catch (Exception e) {
			LOGGER.error("服务器异常!", e);
			resultMap =  ReturnRsUtils.resultBackString(ParameterEunm.FAILED_CODE,null);
		}
		return resultMap;
	}
}
